import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'

export const useRobotStore = defineStore('robot', () => {
  // 机器狗状态
  const robotState = reactive({
    id: 'dog-001',
    name: '机器狗Alpha',
    status: '在线',
    battery: 85,
    position: {
      x: 688,
      y: 363,
      z: 0,
      floor: 6
    },
    posture: 'stand', // stand, sit, lie
    speed: 50,
    isMoving: false,
    direction: null,
    lastUpdate: new Date().toISOString()
  })

  // 摄像头状态
  const cameraState = reactive({
    dogCamera: {
      isActive: true,
      resolution: '1080p',
      fps: 30,
      isRecording: false
    },
    panoramicCamera: {
      isActive: true,
      resolution: '4K',
      pan: 0,
      tilt: 0,
      zoom: 1
    }
  })

  // 任务状态
  const taskState = reactive({
    currentTask: null,
    taskHistory: [],
    isExecuting: false
  })

  // 连接状态
  const connectionState = ref({
    isConnected: true,
    lastHeartbeat: new Date().toISOString(),
    signalStrength: 95
  })

  // 更新机器狗位置
  const updatePosition = (x, y, z = 0) => {
    robotState.position.x = x
    robotState.position.y = y
    robotState.position.z = z
    robotState.lastUpdate = new Date().toISOString()
  }

  // 更新机器狗状态
  const updateStatus = (status) => {
    robotState.status = status
    robotState.lastUpdate = new Date().toISOString()
  }

  // 设置姿态
  const setPosture = (posture) => {
    robotState.posture = posture
    robotState.lastUpdate = new Date().toISOString()
  }

  // 开始移动
  const startMovement = (direction, speed = robotState.speed) => {
    robotState.isMoving = true
    robotState.direction = direction
    robotState.speed = speed
    robotState.status = '移动中'
    robotState.lastUpdate = new Date().toISOString()
  }

  // 停止移动
  const stopMovement = () => {
    robotState.isMoving = false
    robotState.direction = null
    robotState.status = '待命中'
    robotState.lastUpdate = new Date().toISOString()
  }

  // 紧急停止
  const emergencyStop = () => {
    robotState.isMoving = false
    robotState.direction = null
    robotState.status = '紧急停止'
    robotState.lastUpdate = new Date().toISOString()
  }

  // 更新电池电量
  const updateBattery = (level) => {
    robotState.battery = Math.max(0, Math.min(100, level))
    robotState.lastUpdate = new Date().toISOString()
  }

  // 添加任务
  const addTask = (task) => {
    taskState.taskHistory.unshift({
      id: Date.now(),
      ...task,
      timestamp: new Date().toISOString(),
      status: 'pending'
    })
  }

  // 更新任务状态
  const updateTaskStatus = (taskId, status, result = null) => {
    const task = taskState.taskHistory.find(t => t.id === taskId)
    if (task) {
      task.status = status
      task.result = result
      task.completedAt = new Date().toISOString()
    }
  }

  // 设置当前任务
  const setCurrentTask = (task) => {
    taskState.currentTask = task
    taskState.isExecuting = true
  }

  // 完成当前任务
  const completeCurrentTask = (result = null) => {
    if (taskState.currentTask) {
      updateTaskStatus(taskState.currentTask.id, 'completed', result)
      taskState.currentTask = null
      taskState.isExecuting = false
    }
  }

  // 更新摄像头状态
  const updateCameraState = (camera, updates) => {
    Object.assign(cameraState[camera], updates)
  }

  return {
    // 状态
    robotState,
    cameraState,
    taskState,
    connectionState,
    
    // 方法
    updatePosition,
    updateStatus,
    setPosture,
    startMovement,
    stopMovement,
    emergencyStop,
    updateBattery,
    addTask,
    updateTaskStatus,
    setCurrentTask,
    completeCurrentTask,
    updateCameraState
  }
})